﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using iTeachSoft.CoreObjects;
using System.Diagnostics;
using iTeachSoft.Code.Managers;
using iTeachSoft.Presentation.Code.Utility;
using iTeachSoft.Presentation.Dialogs;

namespace iTeachSoft.Presentation.Code.Wrappers
{
    public class AnnotationShape : BasicShape
    {
        #region "Dependency properties"

        public double RadiusX
        {
            get { return (double)GetValue(RadiusXProperty); }
            set { SetValue(RadiusXProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RadiusX.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusXProperty =
            DependencyProperty.Register("RadiusX", typeof(double), typeof(AnnotationShape), null);

        public double RadiusY
        {
            get { return (double)GetValue(RadiusYProperty); }
            set { SetValue(RadiusYProperty, value); }
        }

        // Using a DependencyProperty as the backing store for RadiusY.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty RadiusYProperty =
            DependencyProperty.Register("RadiusY", typeof(double), typeof(AnnotationShape), null);


        public double BorderWeight
        {
            get { return (double)GetValue(BorderWeightProperty); }
            set { SetValue(BorderWeightProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BorderWeight.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BorderWeightProperty =
            DependencyProperty.Register("BorderWeight", typeof(double), typeof(AnnotationShape), null);


        public Brush BackgroundColor
        {
            get { return (Brush)GetValue(BackgroundColorProperty); }
            set { SetValue(BackgroundColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for BackgroundColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty BackgroundColorProperty =
            DependencyProperty.Register("BackgroundColor", typeof(Brush), typeof(AnnotationShape), null);


        public Brush StrokeColor
        {
            get { return (Brush)GetValue(StrokeColorProperty); }
            set { SetValue(StrokeColorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StrokeColor.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StrokeColorProperty =
            DependencyProperty.Register("StrokeColor", typeof(Brush), typeof(AnnotationShape), null);


        #endregion

        public Annotation CoreObject
        {
            get { return mCoreObject as Annotation; }
            set
            {
                mCoreObject = value;
                ReflectCoreObject();
            }
        }

        public AnnotationShape()
            : base()
        {
            //default constructor
            this.Style = (Style)Application.Current.FindResource("AnnotationShapeStyle");
            this.MouseDoubleClick += new MouseButtonEventHandler(AnnotationShape_MouseDoubleClick);
        }

        public AnnotationShape(Annotation coreObject)
            : this()
        {
            CoreObject = coreObject;
        }

        private void ReflectCoreObject()
        {
            RadiusX = (double)CoreObject.RadiusX;
            RadiusY = (double)CoreObject.RadiusY;

            Canvas.SetLeft(this, CoreObject.Bounds.Left);
            Canvas.SetTop(this, CoreObject.Bounds.Top);

            this.Width = CoreObject.Bounds.Width;
            this.Height = CoreObject.Bounds.Height;

            BackgroundColor = new SolidColorBrush(Color.FromArgb(CoreObject.BackColor.A, CoreObject.BackColor.R, CoreObject.BackColor.G, CoreObject.BackColor.B));
            StrokeColor = new SolidColorBrush(Color.FromArgb(CoreObject.Stroke.A, CoreObject.Stroke.R, CoreObject.Stroke.G, CoreObject.Stroke.B)); ;
            BorderWeight = (double)CoreObject.BorderWeight;
        }

        void AnnotationShape_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DialogAnnotationProperties propertiesDialog = new DialogAnnotationProperties();
            propertiesDialog.CoreObject = CoreObject;
            propertiesDialog.ShowDialog();

            ReflectCoreObject();
        }
    }
}
